//
//  ZForgetPasswordVC.m
//  WeClubs
//
//  Created by upintech on 17/6/19.
//  Copyright © 2017年 Zed. All rights reserved.
//

#import "ZForgetPasswordVC.h"
#import "ZCountDownButton.h"
#import <SMS_SDK/SMSSDK.h>

@interface ZForgetPasswordVC ()

@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;

@property (weak, nonatomic) IBOutlet UITextField *paswordTextFiled;

@property (weak, nonatomic) IBOutlet UITextField *verifyTextFiled;

@property (weak, nonatomic) IBOutlet ZCountDownButton *verifyBtn;


@property (weak, nonatomic) IBOutlet UIButton *sureBtn;

@end

@implementation ZForgetPasswordVC

+ (instancetype)zNew
{
    return [self zStoryboardInstanceWithStoryboardName:@"LoginAndRegister"];
}

#pragma mark - life Controller
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [_verifyBtn setVerifyStateTitle:@"获取验证码" andColor:[UIColor zColorWithHex:0x2fcafb]];
    [_verifyBtn setCountDownStateTitle:@"重新获取" andColor:[UIColor zColorWithHex:0x999999]];
    
    self.sureBtn.enabled = NO;
}

#pragma mark - private func
- (void)back {
    
        // 为什么不会退出啊
        [self.navigationController popoverPresentationController];
    

}
#pragma mark - action
//
- (IBAction)getVerifyBtnDidClick:(id)sender {
    
    if (![self.phoneTextField.text zCheckPhoneNumber]) {
        
        [MBProgressHUD zAlertError:kPhoneError];
        return;
    }
    self.sureBtn.enabled = YES;
    
    [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:self.phoneTextField.text zone:@"86" customIdentifier:nil result:^(NSError *error) {
        if (error) {
            ZLog(@"申请短信验证码失败: %@", error);
        } else {
            ZLog(@"短信验证码申请成功");
        }
    }];
    // 进入倒计时
    [self.verifyBtn beginCountDownWithTime:60];
}

// 点击确定
- (IBAction)sureBtnDidClick:(UIButton *)sender {
    
    // 判断手机号码
    if (![_phoneTextField.text zCheckPhoneNumber]) {
        
        [MBProgressHUD zAlertError:kPhoneError];
        return;
    }
    
    // 判断密码
    if (_paswordTextFiled.text.length < 6 || _paswordTextFiled.text.length > 20) {
        
        [MBProgressHUD zAlertError:kPasswordError];
        return;
    }
    // 如果没有验证码
    if (self.verifyTextFiled.text.length < 3) {
        
        [MBProgressHUD zAlertError:kVerifyError];
    }
    
    // forget Key一定要加，因为网络请求中国
    NSDictionary *parameter = @{
                               @"mobile": _phoneTextField.text,
                               @"code": self.verifyTextFiled.text,
                               @"password" : [_paswordTextFiled.text zMD5HexString],
                               @"notoken":@"notoken"
                               };
    [HTTPRequest POST:kChange_passwordUrl parameter:parameter success:^(id resposeObject) {
        
        [MBProgressHUD zAlertSuccess:@"密码修改成功"];
        [self back];
        self.verifyTextFiled.text = @"";
        self.phoneTextField.text = @"";
        self.paswordTextFiled.text = @"";
        
    } failure:^(NSError *error) {
        
       NSString *str = error.userInfo[@"message"];
        [MBProgressHUD zAlertSuccess:str];
    }];
}



@end
